﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YiNing.Tools
{
    public class YiningImage
    {
        private double scaleX = 0.2;
        private double scaleY = 0.2;
        private Image imgTemp;
        private Graphics MyGraphics;
        private int width = 5000;
        private int height = 5000;
        public YiningImage()
        {
            imgTemp = new Bitmap(width, height);
            MyGraphics = Graphics.FromImage(imgTemp);
            MyGraphics.Clear(Color.White);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="w">原始尺寸W</param>
        /// <param name="h">原始尺寸H</param>
        public YiningImage(double w, double h) :this()
        {
            scaleX = width / w;
            scaleY = height / h;
            MyGraphics.Clear(Color.White);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="w">原始尺寸W</param>
        /// <param name="h">原始尺寸H</param>
        /// <param name="color"></param>
        public YiningImage(double w, double h, Color color) : this()
        {
            scaleX = width / w;
            scaleY = height / h;
            MyGraphics.Clear(color);
        }

        /// <summary>
        /// 转换坐标为左下角点
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public Point TransToLeftBottom(Point pt)
        {
            return new Point(pt.X, height - pt.Y);
        }

        //public void DrawRectleByRectangle(Rectangle rectangle, Pen pen = null)
        //{
        //    Rectangle finalRect = new Rectangle(Convert.ToInt32(rectangle.X * scaleX), Convert.ToInt32(rectangle.Y * scaleY), Convert.ToInt32(rectangle.Width * scaleX), Convert.ToInt32(rectangle.Height * scaleY));
        //    if (pen == null) pen = new Pen(Color.Red, 3);
        //    MyGraphics.DrawRectangle(pen, finalRect);
        //}
        /// <summary>
        /// 左下角+右上角
        /// </summary>
        /// <param name="minX"></param>
        /// <param name="minY"></param>
        /// <param name="maxX"></param>
        /// <param name="maxY"></param>
        /// <param name="pen"></param>
        public void DrawRectleByLeftBottomAndRightTop(double minX, double minY, double maxX, double maxY, Pen pen = null)
        {
            DrawRectleByLeftBottomAndRightTop(new Point(Convert.ToInt32(minX), Convert.ToInt32(minY)), new Point(Convert.ToInt32(maxX), Convert.ToInt32(maxY)), pen);
        }
        public void DrawRectleByLeftTopAndRightBottom(double minX, double minY, double maxX, double maxY, Pen pen = null)
        {
            DrawRectleByLeftTopAndRightBottom(new Point(Convert.ToInt32(minX), Convert.ToInt32(minY)), new Point(Convert.ToInt32(maxX), Convert.ToInt32(maxY)), pen);
        }

        /// <summary>
        /// 画箭头
        /// </summary>
        /// <param name="startX"></param>
        /// <param name="endX"></param>
        /// <param name="startY"></param>
        /// <param name="endY"></param>
        /// <param name="direction"></param>
        /// <param name="pen"></param>
        public void DrawLine(double startX, double endX, double startY, double endY, int direction = 0, Pen pen = null)
        {
            if (pen == null) pen = new Pen(Color.Green, 3);
            if (direction == 0) pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; 
            else pen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            DrawLine(new Point(Convert.ToInt32(startX), Convert.ToInt32(startY)), new Point(Convert.ToInt32(endX), Convert.ToInt32(endY)), pen);
        }

        /// <summary>
        /// 左下角+右上角
        /// </summary>
        /// <param name="leftBottom"></param>
        /// <param name="rightTop"></param>
        /// <param name="pen"></param>
        public void DrawRectleByLeftBottomAndRightTop(Point leftBottom, Point rightTop, Pen pen = null)
        {
            var finalLeftBottom = TransToLeftBottom(new Point(Convert.ToInt32(leftBottom.X * scaleX), Convert.ToInt32(leftBottom.Y * scaleY)));
            var finalRightTop = TransToLeftBottom(new Point(Convert.ToInt32(rightTop.X * scaleX), Convert.ToInt32(rightTop.Y * scaleY)));
            Rectangle finalRect = new Rectangle(Convert.ToInt32(finalLeftBottom.X ), Convert.ToInt32(finalRightTop.Y ), Convert.ToInt32((rightTop.X - leftBottom.X) * scaleX), Convert.ToInt32((rightTop.Y - leftBottom.Y) * scaleY));

            //Rectangle finalRect = new Rectangle(Convert.ToInt32(finalLeftBottom.X * scaleX), Convert.ToInt32(finalRightTop.Y * scaleY), Convert.ToInt32((rightTop.X - leftBottom.X)*scaleX), Convert.ToInt32((rightTop.Y - leftBottom.Y)*scaleY));
            if (pen == null) pen = new Pen(Color.Black, 3);
            MyGraphics.DrawRectangle(pen, finalRect);
        }

        /// <summary>
        /// 左上角+右下角
        /// </summary>
        /// <param name="leftTop"></param>
        /// <param name="rightBottom"></param>
        /// <param name="pen"></param>
        public void DrawRectleByLeftTopAndRightBottom(Point leftTop, Point rightBottom, Pen pen = null)
        {
            var finLeftTop = TransToLeftBottom(new Point(Convert.ToInt32(leftTop.X * scaleX), Convert.ToInt32(leftTop.Y * scaleY)));
            var finRightBottom = TransToLeftBottom(new Point(Convert.ToInt32(rightBottom.X * scaleX), Convert.ToInt32(rightBottom.Y * scaleY)));
            Rectangle finalRect = new Rectangle(finLeftTop.X, finLeftTop.Y, finRightBottom.X - finLeftTop.X, finRightBottom.Y - finLeftTop.Y);
            if (pen == null) pen = new Pen(Color.Black, 10);
            MyGraphics.DrawRectangle(pen, finalRect);
        }

        /// <summary>
        /// 写字
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="topLeftX"></param>
        /// <param name="topLeftY"></param>
        /// <param name="fontSize"></param>
        public void DrawString(string txt, double topLeftX, double topLeftY, int fontSize = 10)
        {
            DrawString(txt, new Point(Convert.ToInt32(topLeftX), Convert.ToInt32(topLeftY)), fontSize);
        }
        /// <summary>
        /// 写文字
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="point"></param>
        /// <param name="font"></param>
        public void DrawString(string txt, Point point, int fontSize = 10)
        {
            Font font = new Font("宋体", fontSize, FontStyle.Bold);
            //Brush brush = new Brush();
            MyGraphics.DrawString(txt, font, new SolidBrush(Color.Black), TransToLeftBottom(new Point(Convert.ToInt32(point.X * scaleX), Convert.ToInt32(point.Y * scaleY))));
        }

        /// <summary>
        /// 写文字
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="point"></param>
        /// <param name="font"></param>
        public void DrawLine(Point startP, Point endP, Pen pen = null)
        {
            var finStartP = TransToLeftBottom(new Point(Convert.ToInt32(startP.X * scaleX), Convert.ToInt32(startP.Y * scaleY)));
            var finEndP = TransToLeftBottom(new Point(Convert.ToInt32(endP.X * scaleX), Convert.ToInt32(endP.Y * scaleY)));
            if (pen == null) pen = new Pen(Color.Red, 3);
            MyGraphics.DrawLine(pen, finStartP, finEndP);
        }

        /// <summary>
        /// 画点
        /// </summary>
        /// <param name="topLeftX"></param>
        /// <param name="topLeftY"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="pen"></param>
        public void DrawCircle(double topLeftX, double topLeftY, double width, double height, Pen pen= null)
        {
            if (pen == null) pen = new Pen(Color.Red, 3);
            var point = TransToLeftBottom(new Point(Convert.ToInt32(topLeftX * scaleX), Convert.ToInt32(topLeftY * scaleY)));
            //MyGraphics.DrawEllipse(new Pen(color, width), )
            MyGraphics.DrawEllipse(pen, point.X, point.Y, Convert.ToSingle(width * scaleX), Convert.ToSingle(height * scaleY));
        }


        /// <summary>
        /// 描绘@图像@信息@
        /// </summary>
        /// <param name="text"></param>
        /// <param name="imageCenter"></param>
        /// <param name="pen"></param>
        public void DrawDieInfo(string text, Point leftTop, Point rightBottom, Pen pen = null)
        {
            if (pen == null) pen = new Pen(Color.Yellow, 5);
            DrawRectleByLeftTopAndRightBottom(leftTop, rightBottom, pen);
            DrawString(" " + text, leftTop, 10);
        }

        public void Save(string filePath)
        {
            try
            {
                imgTemp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception er) { }
        }
     }
}
